<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=$params2=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['storeids']){
		$params['storeid']=$_SESSION['storeids'];
	}
	if($_RQ['storeid']){
		$params['storeid']=$_RQ['storeid'];
		$params2['storeid']=$_RQ['storeid'];
		$return['params']['storeid']=$_RQ['storeid'];
	}
	$params['status']=intval($_RQ['status']);
	$return['params']['status']=intval($_RQ['status']);
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mobile']){
		$params2['mobile like']='%'.trim($_RQ['mobile']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['tuidaytime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:50;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_tuifee_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=$this->model('member')->member_show($item['memberid']);
			if($item['status']==4){
				$isfeelog=pdo_get('jy_yxw_feelog',array('tuifeeid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				if(!$isfeelog['id']){
					$this->model('feelog')->upfeelog($item['id'],'',$item['type']==2?4:3);
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'member','title'=>'客户信息','type'=>'member'),
			array('type'=>'unitstring','unit'=>'元','title'=>'退费金额','name'=>'fee'),
			array('type'=>'unitstring','unit'=>'元','title'=>'已收取金额','name'=>'isfee'),
			array('name'=>'remark','title'=>'退费原因','type'=>'string'),
			array('type'=>'string','title'=>'实际退费金额','name'=>'tuifee'),
			array('type'=>'string','title'=>'退费账户','name'=>'zhanghu'),
			array('type'=>'string','title'=>'退费日期','name'=>'tuiday'),
			array('name'=>'fee1','title'=>'总部退费分摊','type'=>'unitstring','unit'=>'元'),
			array('name'=>'fee2','title'=>'售后基地退费分摊','type'=>'unitstring','unit'=>'元'),
			array('name'=>'fee3','title'=>'销售门店退费分摊','type'=>'unitstring','unit'=>'元'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','action'=>'/store.tuifee/look/','title'=>'查看详情'),
			array('type'=>'href','action'=>'/store.tuifee/check/','title'=>'审批','show'=>array('status'=>1)),
		)),
		'search'=>array(
			array('name'=>'storeid','width'=>10,'title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'title','width'=>10,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>10,'title'=>'电话','type'=>'string'),
			array('name'=>'createtime','width'=>15,'title'=>'申请时间','type'=>'time_range'),
		),
	);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['stores']=$this->getall('store',$params,array('id','title'),'id');
	$return['status']=$this->tuifee_status;
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='look'){
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_tuifee_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
	);
	$htmls['look']=array(
		array('type'=>'tip','title'=>'发起“退成交”申请，成交成功后退成交，向“店长”发起，店长确认达到“财务”系统。'),
		array('type'=>'string','title'=>'售后姓名','name'=>'title'),
		array('type'=>'string','unit'=>'元','title'=>'退费金额','name'=>'fee'),
		array('type'=>'string','unit'=>'元','title'=>'已收取金额','name'=>'isfee'),
		array('type'=>'image','title'=>'退成交协议图片','name'=>'tuiimage'),
		array('type'=>'text','title'=>'退成交原因','name'=>'remark'),
		array('name'=>'tuifee','title'=>'实际退费金额','type'=>'string'),
		array('name'=>'zhanghu','title'=>'退费账户','type'=>'string'),
		array('name'=>'tuiday','title'=>'退费日期','type'=>'string'),
		array('name'=>'fee1','title'=>'补贴费退费分摊','type'=>'string'),
		array('name'=>'fee2','title'=>'学校退费分摊','type'=>'string'),
		array('name'=>'fee3','title'=>'销售门店退费分摊','type'=>'string'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if($post['check'] && !$post['remark14']){
			exi('请填写审核不通过的原因！','error');
		}
		$id=$post['id'];
		$update=array(
			'fee1'=>$post['fee1'],
			'fee2'=>$post['fee2'],
			'fee3'=>$post['fee3'],
			'zhanghu'=>$post['zhanghu'],
			'tuifee'=>$post['tuifee'],
			'tuiday'=>$post['tuiday'],
			'remark1'=>trim($post['remark1']),
			'status'=>$post['check']?3:4,
		);
		pdo_update('jy_yxw_tuifee_log',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$update=array('up_time'=>time(),'status'=>$post['check']?104:105);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['memberid']));
		$update=array('up_time'=>time(),'status'=>$post['check']?104:105);
		pdo_update('jy_yxw_yonghu',$update,array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$post['memberid']));
		if(!$post['check']){
			$data=array(
				'fee1'=>$post['fee1'],
				'fee2'=>$post['fee2'],
				'fee3'=>$post['fee3'],
				'zhanghu'=>$post['zhanghu'],
				'remark'=>trim($post['remark']),
				'status'=>1,
				'noadd'=>1,
			);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['yonghuid']=$id;
			$data['memberid']=$post['memberid'];
			$data['storeid']=$post['storeid'];
			$data['up_time']=time();
			$data['createtime']=time();
			$data['up_workerid']=$_SESSION['workerid'];
			$this->insert('fentan_log',$data);
		}
		exi('操作成功！','','jy_yxw/store.tuifee//status=0');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_tuifee_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['member']=$this->model('member')->member_show($item['memberid']);
	}else{
		exi('请先选择客户','error');
	}
	$item['check']=0;
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'审核退成交',
	);
	$htmls['edit']=array(
		array('type'=>'tip','title'=>'发起“退成交”申请，成交成功后退成交，向“店长”发起，店长确认达到“财务”系统。'),
		array('type'=>'copy','title'=>'售后姓名','name'=>'member.nickname'),
		array('type'=>'copy','title'=>'退费金额','name'=>'fee'),
		array('type'=>'copy','title'=>'已收取金额','name'=>'isfee'),
		array('type'=>'copy','title'=>'退成交原因','name'=>'remark'),
		array('type'=>'string_two','title'=>'退费','title1'=>'实际退费金额','title2'=>'退费账户','name1'=>'tuifee','name2'=>'zhanghu'),
		array('type'=>'time','title'=>'退费日期','name'=>'tuiday','time_type'=>'day'),
		array('name'=>'fee1','title'=>'总部退费分摊','type'=>'unitstring','unit'=>'元'),
		array('name'=>'fee2','title'=>'售后基地退费分摊','type'=>'unitstring','unit'=>'元'),
		array('name'=>'fee3','title'=>'销售门店退费分摊','type'=>'unitstring','unit'=>'元'),
		array('type'=>'radio','title'=>'审批','name'=>'check','options'=>'check'),
		array('type'=>'text','title'=>'审核不通过原因','name'=>'remark14','show'=>array('check'=>1)),
	);
	$return['check']=array('审核通过','审核不通过');
	$return['htmls']=$htmls;
	exi($return);
}